/**
 * MarketData.java
 * Description: ...
 * Created by: rollgan
 * Created time: 2015-04-13 17:07:37
 * Copyright 2015 www.magnostadt.com
 */
package com.qit.prototype.flink.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author rollgan
 *
 */
public class MarketData {
	public static final String MARKET_DATA_KEY = "MARKET_DATA_KEY";
	public static final String FIELD_GROUP_TRADE = "TRADE";
	public static final String FIELD_GROUP_DEPTH = "DEPTH";
	public static final String TICK_MD_BACKUP_CODE = "TICK_MD_CODE@BACKUP";

	private Long id;
	private String code;
	private String tradingType;
	private String fieldGroup;
	private String fieldGroupSub;
	private Map<String, String> fieldValues;
	private Date timeInLocal;
	private Long timeInGmt;
	private String sourceFrom;
	private String marketIndicator;
	private Boolean realTimeUndertakeFlag;
	private String partyRoleQualifier;
	private String marketDataKey;
	private long sourceTime = getMicTime();
	private long generateTime = sourceTime;
	private static long timeDiff = 0;
	
	public MarketData(String code, String fieldGroup) {
		this.code = code;
		this.fieldGroup = fieldGroup;
		this.fieldValues = new HashMap<String, String>();
		this.marketDataKey = buildMarketDataKey(this.code, this.fieldGroup);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	public String getTradingType() {
		return tradingType;
	}

	public void setTradingType(String tradingType) {
		this.tradingType = tradingType;
	}

	/**
	 * @return the fieldGroup
	 */
	public String getFieldGroup() {
		return fieldGroup;
	}

	/**
	 * @param fieldGroup the fieldGroup to set
	 */
	public void setFieldGroup(String fieldGroup) {
		this.fieldGroup = fieldGroup;
	}

	public String getFieldGroupSub() {
		return fieldGroupSub;
	}

	public void setFieldGroupSub(String fieldGroupSub) {
		this.fieldGroupSub = fieldGroupSub;
	}

	/**
	 * @return the fieldValues
	 */
	public Map<String, String> getFieldValues() {
		return fieldValues;
	}

	/**
	 * @param fieldValues the fieldValues to set
	 */
	public void setFieldValues(Map<String, String> fieldValues) {
		this.fieldValues = fieldValues;
	}

	/**
	 * @return the timeInLocal
	 */
	public Date getTimeInLocal() {
		return timeInLocal;
	}

	/**
	 * @param timeInLocal the timeInLocal to set
	 */
	public void setTimeInLocal(Date timeInLocal) {
		this.timeInLocal = timeInLocal;
	}

	/**
	 * @return the timeInGmt
	 */
	public Long getTimeInGmt() {
		return timeInGmt;
	}

	/**
	 * @param timeInGmt the timeInGmt to set
	 */
	public void setTimeInGmt(Long timeInGmt) {
		this.timeInGmt = timeInGmt;
	}

	/**
	 * @return the sourceFrom
	 */
	public String getSourceFrom() {
		return sourceFrom;
	}

	/**
	 * @param sourceFrom the sourceFrom to set
	 */
	public void setSourceFrom(String sourceFrom) {
		this.sourceFrom = sourceFrom;
	}

	public String getMarketIndicator() {
		return marketIndicator;
	}

	public void setMarketIndicator(String marketIndicator) {
		this.marketIndicator = marketIndicator;
	}

	public Boolean getRealTimeUndertakeFlag() {
		return realTimeUndertakeFlag;
	}

	public void setRealTimeUndertakeFlag(Boolean realTimeUndertakeFlag) {
		this.realTimeUndertakeFlag = realTimeUndertakeFlag;
	}

	public String getPartyRoleQualifier() {
		return partyRoleQualifier;
	}

	public void setPartyRoleQualifier(String partyRoleQualifier) {
		this.partyRoleQualifier = partyRoleQualifier;
	}

	public String getMarketDataKey() {
		return this.marketDataKey;
	}
	
	public static String buildMarketDataKey(String code, String fieldGroup) {
		return fieldGroup + "@" + code;
	}

	public static Integer buidUniqueCode(MarketData marketData) {
		Integer tmpCode = marketData.toString().hashCode();
		return tmpCode;
	}

//	public static String toString(MarketData marketData) {
//		ObjectMapper mapper = new ObjectMapper();
//		try {
//			return mapper.writeValueAsString(marketData);
//		} catch (JsonProcessingException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
	
	public long getSourceTime() {
		return sourceTime;
	}

	public void setSourceTime(long sourceTime) {
		this.sourceTime = sourceTime;
	}

	public long getGenerateTime() {
		return generateTime;
	}

	public void setGenerateTime(long generateTime) {
		this.generateTime = generateTime;
	}

	public static Long getMicTime() {
		Long nanoTime = System.nanoTime(); // 纳秒
		if(timeDiff == 0) {
			long mt = System.currentTimeMillis();
			timeDiff = ((mt) - (nanoTime / 1000000)) * 1000;
		}
		return (timeDiff + nanoTime / 1000);
	}
}
// 1556714890863
// 961069 405923232

//1556714924471
// 961103 013351 693
